package com.yunzheng.controller;

import com.yunzheng.common.BaseResult;
import com.yunzheng.constants.SystemConstants;
import com.yunzheng.dto.LoginDTO;
import com.yunzheng.dto.PasswordForgetDTO;
import com.yunzheng.dto.RegisterDTO;
import com.yunzheng.enums.ResultStatuesEnums;
import com.yunzheng.exception.ServiceException;
import com.yunzheng.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author 郭思良
 * @create 2020-08-15 22:24
 */


@Api(tags = "登录注册")
@Slf4j
@RestController
@RequestMapping("/user")
public class SysLoginController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 1.发送注册用的验证码
     * 根据用户手机号发送验证码
     *
     * /register/phone -- anon
     *
     * @param userPhone 手机号
     */
    @ApiOperation(value = "注册：发送手机验证码")
    @GetMapping("/register/phone")
    public BaseResult registerPhone(String userPhone) {
        if (!java.util.regex.Pattern.matches(SystemConstants.PHONE_REGEX, userPhone)){
            throw new ServiceException(ResultStatuesEnums.PHONE_REGEX_NOT_EXIST);
        }
        sysUserService.sentVerification(userPhone);
        return BaseResult.ok();
    }

    /**
     * 2.完成注册
     *
     * /register -- anon
     *
     * @param registerDTO 验证对象
     */
    @ApiOperation(value = "注册：完成注册")
    @PostMapping("/register")
    public BaseResult register(@Valid @RequestBody RegisterDTO registerDTO) {
        // TODO: 2020/8/9 team什么时候存？注册的时候让他们选team，选完的时候直接进入项目组
        sysUserService.register(registerDTO);
        return BaseResult.ok();
    }

    /**
     * 3.登录
     *
     * /login -- anon
     *
     * @return token
     */
    @ApiOperation(value = "用户状态：登录")
    @PostMapping("/login")
    public BaseResult login(@Valid @RequestBody LoginDTO input) {
        return BaseResult.ok(sysUserService.login(input.getUserPhone(), input.getPassword()));
    }

    /**
     * 4.退出登录
     *
     * /logout -- authc
     *
     * @param request 请求
     * @return 返回
     */
    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public BaseResult logout(HttpServletRequest request) {
        sysUserService.outLogin(request);
        return BaseResult.ok();
    }

    /**
     * 5.发送忘记密码用的验证码
     * 给当前登录用户发送验证码
     *
     * /forgetPWD/phone -- anon
     *
     * @param userPhone 用户手机号
     */
    @ApiOperation(value = "忘记密码：发送手机验证码")
    @GetMapping("/forgetPWD/phone")
    public BaseResult forgetRegister(String userPhone) {
        if (!java.util.regex.Pattern.matches(SystemConstants.PHONE_REGEX, userPhone)){
            throw new ServiceException(ResultStatuesEnums.PHONE_REGEX_NOT_EXIST);
        }
        sysUserService.forgetVerification(userPhone);
        return BaseResult.ok();
    }

    /**
     * 6.忘记密码
     *
     * /forgetPWD -- anon
     *
     * @param passwordForgetDTO 密码信息对象
     */
    @ApiOperation(value = "忘记密码：完成重设密码")
    @PutMapping("/forgetPWD")
    public BaseResult forgetPassword(@Valid @RequestBody PasswordForgetDTO passwordForgetDTO) {
        sysUserService.forgetPSW(passwordForgetDTO.getUserPhone(), passwordForgetDTO.getNewPasswd(), passwordForgetDTO.getCaptcha(), passwordForgetDTO.getNewPasswdTwo());
        return BaseResult.ok();
    }
}
